﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IHISCETeim.DAL;
using IHISCETeim.Model;
using IHISCETeim.Library;
using System.Data;


namespace IHISCETeim.BLL
{
    public  class JChuJiaGongWuListController
    {
        public static SEMailListInfo GetEmail(int ChuJGWId)
        {
            SEMailListInfo objEmail = new SEMailListInfo();
            JChuJiaGongWuListInfo obj = GetListByID(ChuJGWId);
            if (obj.CurrFlowUserId == 0)
                return null;
            // AgentQltyInfo obj = AgentQltyController.GetByQltyId(QltyId);
            OUserInfo svrObj = OUserController.GetById(obj.CurrFlowUserId);
            objEmail.MailTo = svrObj.EMail;
            objEmail.Subject = string.Format("您有初加工物管制需要承办{0}", obj.DocNum);
            objEmail.Body = string.Format("编 号:<b>{0}</b><br/>", obj.DocNum);
            objEmail.Body += string.Format("发行人:<b>{0}</b><br/>", obj.AddRealName);
            objEmail.Body += string.Format("供应商:<br/><b>{0}</b><br/>", obj.SupplerName);
            objEmail.Body += "<br/>==============================================================================<br/>";
            objEmail.Body += "<br/>请进入经销商终端平台初加工物管制票进行处理.<br/>";
            objEmail.Body += "<BR/><BR/>以上内容为 IHISCE 信息平台系统自动发送，请勿回复.";

            return objEmail;
        }
        /// <summary>
        /// 新增初加工管制票
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int Add(JChuJiaGongWuListInfo obj, List<JChuJiaItemDetailInfo> itemList, List<JChuJiaSerialDetailInfo> serialList, List<JChuJiaFlowDetailInfo> flowList)
        {
            int ChuJGWId = 0;
            ChuJGWId=JChuJiaGongWuListDAL.Add(obj, itemList, serialList, flowList);
            if (ChuJGWId > 0) //发邮件
            {
                SEMailListInfo objEmail = GetEmail(ChuJGWId);
                if(objEmail!=null)
                    SEMailListDAL.Add(objEmail);
            }
            return ChuJGWId;
        }

        public static bool Update(JChuJiaGongWuListInfo obj, List<JChuJiaItemDetailInfo> itemList, List<JChuJiaSerialDetailInfo> serialList)
        {
            return JChuJiaGongWuListDAL.Update(obj, itemList, serialList);
        }

        /// <summary>
        /// 删除初加工管理表
        /// </summary>
        /// <param name="ChuJGWId"></param>
        /// <returns></returns>
        public static bool Delete(int ChuJGWId)
        {
            return JChuJiaGongWuListDAL.Delete(ChuJGWId);
        }

        public static List<JChuJiaFlowDetailInfo> GetAllJChuJiaFlowDetail(int ChuJGWId)
        {
            return  CBO.FillCollection<JChuJiaFlowDetailInfo>( JChuJiaGongWuListDAL.GetAllJChuJiaFlowDetail(ChuJGWId));
        }
        public static List<JChuJiaItemDetailInfo> GetAllJChuJiaItemDetailByChuJGWId(int ChuJGWId)
        {
            return CBO.FillCollection<JChuJiaItemDetailInfo>(JChuJiaGongWuListDAL.GetAllJChuJiaItemDetailByChuJGWId(ChuJGWId));
        }
        public static List<JChuJiaSerialDetailInfo> GetAllJChuJiaSerialDetailByChuJGWId(int ChuJGWId)
        {
            return CBO.FillCollection<JChuJiaSerialDetailInfo>(JChuJiaGongWuListDAL.GetAllJChuJiaSerialDetailByChuJGWId(ChuJGWId));
        }
        public static List<JChuJiaTempFlowInfo> GetAllJChuJiaTempFlow()
        {
            return CBO.FillCollection<JChuJiaTempFlowInfo>(JChuJiaGongWuListDAL.GetAllJChuJiaTempFlow());
        }

        public static JChuJiaGongWuListInfo GetListByID(int ChuJGWId)
        {
            return CBO.FillObject<JChuJiaGongWuListInfo>(JChuJiaGongWuListDAL.GetListByID(ChuJGWId));
        }
        /// <summary>
        /// 获取单实例流程信息
        /// </summary>
        /// <param name="ChuFlowID"></param>
        /// <returns></returns>
        public static JChuJiaFlowDetailInfo GetListJChuJiaFlowDetail(int ChuFlowID)
        {
            return CBO.FillObject<JChuJiaFlowDetailInfo>(JChuJiaGongWuListDAL.GetListJChuJiaFlowDetail(ChuFlowID));
        }
        public static DataTable GetPagination(string whereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            return JChuJiaGongWuListDAL.GetPagination(whereSql, PageIndex, PageSize, Sort, ref  TotalCount);
        }
        /// <summary>
        /// 更新单个实例流程信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool UpdateJChuJiaFlowDetail(JChuJiaFlowDetailInfo obj,List<JChuJiaSerialDetailInfo> listSerial)
        {
            //obj.IsSend = obj.IsSend ? true : false;
            bool x= JChuJiaGongWuListDAL.UpdateJChuJiaFlowDetail(obj, listSerial);
            if (x && !obj.IsSend) //发邮件
            {
                SEMailListInfo objEmail = GetEmail(obj.ChuJGWId);
                if (objEmail != null)
                    SEMailListDAL.Add(objEmail);

               
            }

            return x;

            
        }
    }
}
